using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Lizk.Utilities
{
    /// <summary>
    /// A wrapper class for simple XML serialization of objects, for use in settings files.
    /// </summary>
    public class XmlSettings
    {
        /// <summary>
        /// Loads a serialized object of the defined type from the file at the specified path
        /// </summary>
        /// <typeparam name="T">The type of object contained in the file.</typeparam>
        /// <param name="file">File that contains the serialized object.</param>
        /// <returns>The Deserialized object</returns>
        public static T loadXml<T>(string file)
        {
            T settings;
            // Construct an instance of the XmlSerializer with the type
            // of object that is being deserialized.
            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            // To read the file, create a FileStream.
            FileStream myFileStream = new FileStream(file, FileMode.Open);
            // Call the Deserialize method and cast to the object type.
            settings = (T)mySerializer.Deserialize(myFileStream);
            myFileStream.Close();
            return settings;
        }/*
        /// <summary>
        /// XML Serializes an object and saves it to the target path.
        /// </summary>
        /// <typeparam name="T">The type of object to serialize.</typeparam>
        /// <param name="obj">The object to serialized.</param>
        /// <param name="targetFile">Where to save the serialized object</param>
        public static void saveXml<T>(T obj, string targetFile)
        {
                XmlSerializer mySerializer = new XmlSerializer(typeof(T));
                // To write to a file, create a StreamWriter object.
                StreamWriter myWriter = new StreamWriter(targetFile);
                mySerializer.Serialize(myWriter, obj);
                myWriter.Close();
        }*/

        /// <summary>
        /// XML Serializes an object and saves it to the target path.
        /// </summary>
        /// <param name="obj">The object to serialized.</param>
        /// <param name="targetFile">Where to save the serialized object</param>
        public static void saveXml(object obj, string targetFile)
        {
            XmlSerializer mySerializer = new XmlSerializer(obj.GetType());
            // To write to a file, create a StreamWriter object.
            StreamWriter myWriter = new StreamWriter(targetFile);
            mySerializer.Serialize(myWriter, obj);
            myWriter.Close();
        }

        /// <summary>
        /// XML Serializes an object and saves it to the target path.
        /// </summary>
        /// <param name="obj">The object to serialized.</param>
        /// <param name="targetFile">Where to save the serialized object</param>
        /// <param name="extraTypes">Types to include</param>
        public static void saveXml(object obj, string targetFile, Type[] extraTypes)
        {
            XmlSerializer mySerializer = new XmlSerializer(obj.GetType(),extraTypes);
            // To write to a file, create a StreamWriter object.
            StreamWriter myWriter = new StreamWriter(targetFile);
            mySerializer.Serialize(myWriter, obj);
            myWriter.Close();
        }
        /// <summary>
        /// Loads a serialized object of the defined type from the file at the specified path
        /// </summary>
        /// <param name="file">File that contains the serialized object.</param>
        /// <param name="extraTypes">Types to include</param>
        /// <returns>The Deserialized object</returns>
        public static object loadXml(string file, Type[] extraTypes)
        {
            if (extraTypes.Length < 1)
                return null;
                
            // Construct an instance of the XmlSerializer with the type
            // of object that is being deserialized.
            XmlSerializer mySerializer = new XmlSerializer(extraTypes[0], extraTypes);
            // To read the file, create a FileStream.
            FileStream myFileStream = new FileStream(file, FileMode.Open);
            // Call the Deserialize method and cast to the object type.
            object settings = mySerializer.Deserialize(myFileStream);
            myFileStream.Close();
            return settings;
        }

    }
}
